﻿using System;
using System.Collections.Generic;
using AChance.Bll.Func;
using AChance.Bll.Trans;
using AChance.Func;
using FutureSerWeb.Controllers.Base;
using FutureSerWeb.Func;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace FutureSerWeb.Controllers
{
    public class AppController : BaseController
    {
        private static readonly BllTrans.Code.CheckStrategy invokeCheckStrategy = new BllTrans.Code.CheckStrategy()
        {
            NoNeedSession = new List<string>()
            {
                BllTrans.Code.Type.GET,
                BllTrans.Code.Extract<TrnBuilder>(),
                BllTrans.Code.Extract<TrnBuilder.GetList>()
            }
        };
        [HttpPost]
        public object Invoke([FromBody] AppService.ReqPkg reqPkg)
        {
            try
            {
                //检查请求包是否有效
                if (reqPkg is null)
                {
                    reqPkg = new AppService.ReqPkg();
                    throw new Exception("请求包无效");
                }
                reqPkg.OutState = string.Empty;
                //检查交易码是否有效
                if (!invokeCheckStrategy.IsInclude(reqPkg.InTransCode))
                    throw new Exception("交易码无效");
                //检查会话是否有效
                if (BllSession.Exists(reqPkg.InSession))
                {
                    //更新最后一次请求信息
                    if (!BllSession.UpdateLastReqInfo(reqPkg.InSession, reqPkg.InTransCode, JsonConvert.SerializeObject(reqPkg.InData)))
                    {
                        reqPkg.OutState = AppService.State.FREQUENT_REQ;
                        throw new Exception(string.Empty);
                    }
                    //获取状态信息
                    reqPkg.InStateInfo = BllSession.GetStateInfo(reqPkg.InSession);
                }
                else
                {
                    //判断是否需要会话
                    if (invokeCheckStrategy.IsNeedSession(reqPkg.InTransCode))
                    {
                        reqPkg.OutState = AppService.State.INVALID_SESSION;
                        throw new Exception(string.Empty);
                    }
                    //创建状态信息
                    reqPkg.InStateInfo = new BllVar.StateInfo();
                    //检查App是否有效
                    if (!BllApp.Exists(reqPkg.InAppName, reqPkg.InAppType))
                        throw new Exception("App无效");
                    reqPkg.InStateInfo.AppName = reqPkg.InAppName;
                    reqPkg.InStateInfo.AppType = reqPkg.InAppType;
                    reqPkg.InStateInfo.AppFullName = reqPkg.InAppName + reqPkg.InAppType;
                    //检查App版本是否有效
                    if (!BllApp.CheckVer(reqPkg.InAppName, reqPkg.InAppType, reqPkg.InAppVer))
                        throw new Exception("App版本无效");
                    reqPkg.InStateInfo.AppVer = reqPkg.InAppVer;
                    //检查App编码是否有效
                    reqPkg.InStateInfo.AppCode = reqPkg.InAppCode;
                    //检查机构编号是否有效
                    if (!reqPkg.InOrganNo.Equals(BllCache.Organ.GetNo()))
                        throw new Exception("机构编号无效");
                    reqPkg.InStateInfo.OrganNo = BllCache.Organ.GetNo();
                    reqPkg.InStateInfo.OrganName = BllCache.Organ.GetName();
                }
                //调用交易
                if (!BllTrans.Invoke(reqPkg, null))
                {
                    reqPkg.OutState = string.Empty;
                    throw new Exception(reqPkg.OutMsg);
                }
                reqPkg.OutState = AppService.State.SUCCESS;
            }
            catch (Exception ex)
            {
                if (reqPkg.OutState.IsNullOrEmpty())
                    reqPkg.OutState = AppService.State.FAIL;
                reqPkg.OutMsg = ex.Message.IsNullOrEmpty() ? AppService.State.GetDesc(reqPkg.OutState) : ex.Message;
            }
            //返回应答内容
            return reqPkg.GetRespContent();
        }
    }
}
